/**
 * 
 */
package xin.tianhui.cloud.weblog.util;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.util.UrlPathHelper;

/**
 * url 帮助类
 * @author junchen
 *
 */
public class URLHelper {
	public static String getURI(HttpServletRequest request) {
		UrlPathHelper helper = new UrlPathHelper();
		String uri = helper.getOriginatingRequestUri(request);
		String ctx = helper.getOriginatingContextPath(request);
		if (!StringUtils.isBlank(ctx)) {
			uri= uri.substring(ctx.length());
		}
		//跨路径伪请求
		if(uri.contains("../")||uri.contains("..\\")){
			return "";
		}
		return uri;
	}
	
	public static String[] getPaths(String uri) {
		if (uri == null) {
			throw new IllegalArgumentException("URI can not be null");
		}
		if (!uri.startsWith("/")) {
			throw new IllegalArgumentException("URI must start width '/'");
		}
		String[] paths = StringUtils.split(uri, '/');
		return paths;
	}
	
	/**
	 * 获得路径信息
	 * 
	 * @param request
	 * @return
	 */
	public static String[] getPaths(HttpServletRequest request) {
		return getPaths(getURI(request));
	}
}
